package com.movisol.messengercommunicator.asynctasks;

import android.os.AsyncTask;
import android.widget.Toast;

import com.movisol.messengercommunicator.controllers.CommunicatorController;
import com.movisol.messengercommunicator.helper.MessengerCommunicatorHelper;
import com.movisol.messengerprofileservice.interfaces.errorcodes.SaveUserProfileErrorCodes;
import com.movisol.tools.HelperUtils;

public class AsyncLoadConversationsList extends AsyncTask<Void, Void, Void> {

	private CommunicatorController cc = CommunicatorController.getInstance();
	
	@Override
	protected Void doInBackground(Void... params) {
		
		String result =  cc.getXMLfromUri(cc.getConversationCache().getUri());
		cc.setConversationsList(MessengerCommunicatorHelper.parseXMLConversationListToObject(result));
		
		return null;
	}

	@Override
	protected void onPostExecute(Void result) {
		super.onPostExecute(result);
		
		if(cc.getConversationsList() != null)
			cc.fireConversationsListLoaded(null);
		else
		{
			HelperUtils.LogE(getClass(), "cc.getConversationsList() is NULL");
			HelperUtils.showToastMessage(cc.getContext(), SaveUserProfileErrorCodes.getErrorByCode(SaveUserProfileErrorCodes.INTERNAL_ERROR), Toast.LENGTH_LONG);
		}
	}
}